#include <iostream>
using namespace std;
struct node {
    int date;
    node* prev;
    node* next;
    };
class doublelinks {
    public:
        doublelinks();
        ~doublelinks();
    public:
        int doublelink_insert(doublelinks* ptr, int position, int member);
        int doublelink_erase(doublelinks* ptr, int position);
        void doublelink_display(doublelinks* ptr, int num);
        int doublelink_getlength(doublelinks* ptr);
    private:
        int length;
        node* root;

    };

doublelinks::doublelinks() {
    root = new node;
    root->prev = NULL;
    root->next = NULL;
    length = 0;
    }

doublelinks::~doublelinks() {
    }

int doublelinks::doublelink_insert(doublelinks *ptr, int position, int member) {
    node* nodeinsert = new node;
    nodeinsert->date = member;
    if (ptr->doublelink_getlength(ptr) == 0) {
        root->next = nodeinsert;
        nodeinsert->prev = nodeinsert;
        nodeinsert->next = nodeinsert;
        ptr->length++;
        return 0;
        }
    else {
        if (position == 0) {
            nodeinsert->prev = root->next->prev;
            nodeinsert->next = root->next;
            root->next->prev->next = nodeinsert;
            root->next->prev = nodeinsert;
            root->next = nodeinsert;
            ptr->length++;
            return 0;
            }
        else {
            node* current = root->next;
            for (int i = 0; i < position; i++) {
                current = current->next;
                }
            nodeinsert->next = current;
            nodeinsert->prev = current->prev;
            current->prev->next = nodeinsert;
            current->prev = nodeinsert;
            ptr->length++;
            return 0;
            }
        }
    }

int doublelinks::doublelink_erase(doublelinks* ptr, int position) {
    if (ptr->doublelink_getlength(ptr) == 0) {
        cout << "链表为空，oop！！" << endl;
        return 0;
        }
    else {
        if (ptr->doublelink_getlength(ptr) == 1) {
            ptr->root->next = NULL;
            ptr->length--;
            }
        else {
            node* deletenode = root->next;
            for (int i = 0; i < position; i++) {
                deletenode = deletenode->next;
                }
            deletenode->prev->next = deletenode->next;
            deletenode->next->prev = deletenode->prev;
            delete deletenode;
            ptr->length--;
            }
        }
    }
int doublelinks::doublelink_getlength(doublelinks* ptr) {
    return ptr->length;
    }
void doublelinks::doublelink_display(doublelinks* ptr, int num) {
    node* current = root->next;
    for (int i = 0; i < num; i++) {
        cout << current->date << " ";
        current = current->next;
        }
    cout << endl;
    }

int main() {
    doublelinks* doublelink_ptr = new doublelinks;
    for (int i = 0; i < 10; i++) {
        doublelink_ptr->doublelink_insert(doublelink_ptr, 0, i);
        }
    doublelink_ptr->doublelink_display(doublelink_ptr, 20);
    doublelink_ptr->doublelink_erase(doublelink_ptr, 2);
    doublelink_ptr->doublelink_display(doublelink_ptr, 20);
    return 0;
    }
